// pages/addAddress/addAddress.js
const { addAddress } = require('../../utils/http')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 省
    provinceList: [],
    showPrivincePopip: false,
    province: {},
    haveProvince: false,
    // 市
    cityList: [],
    showCityPopip: false,
    city: {},
    // 县
    countyList: [],
    showCountyPopip: false,
    county: {},

    userName: '',
    phone: '',
    address: ''
  },
  showPrivince() {
    this.setData({
      showPrivincePopip: true
    })
  },
  // hidePrivince() {
  //   this.setData({
  //     showPrivincePopip: false
  //   })
  // },
  choseProvince(e) {
    console.log(e.detail)
    const { value, index } = e.detail;
    this.setData({
      province: value,
      showPrivincePopip: false,
      haveProvince: true,
    })
    this.getProvinceChild()
  },
  // /common/region/v2/child
  getChild(id) {
    const data = ''
    wx.request({
      url: `https://api.it120.cc/common/region/v2/child?pid=${id}`,
      success(res) {
        console.log(res.data)
        that.setData({
          cityList: res.data.data
        })
      }
    })
  },
  getProvinceChild() {
    const that = this
    wx.request({
      url: `https://api.it120.cc/common/region/v2/child?pid=${this.data.province.id}`,
      success(res) {
        console.log(res.data)
        that.setData({
          cityList: res.data.data
        })
      }
    })
  },
  showPrivinceChild() {
    this.setData({
      showCityPopip: true
    })
  },
  choseCity(e) {
    const that = this
    console.log(e.detail)
    const { value, index } = e.detail;
    wx.request({
      url: `https://api.it120.cc/common/region/v2/child?pid=${value.id}`,
      success(res) {
        console.log(132)
        console.log(res.data)
        that.setData({
          countyList: res.data.data
        })
        console.log(that.data.countyList)
      }
    })
    this.setData({
      showCityPopip: false,
      city: value
    })
  },
  choseCounty(e) {
    const { value, index } = e.detail;
    this.setData({
      county: value,
      showCountyPopip: false
    })
  },
  onCancel() {
    this.setData({
      showPrivincePopip: false,
      showCityPopip: false,
      showCountyPopip: false
    })
  },
  showCounty() {
    this.setData({
      showCountyPopip: true
    })
  },
  getUserName(e) {
    const val = e.detail
    this.setData({
      userName: val
    })
  },
  getPhone(e) {
    const val = e.detail
    this.setData({
      phone: val
    })
  },
  getAddress(e) {
    const val = e.detail
    this.setData({
      address: val
    })
  },
  saveAddress() {
    // const token = wx.getStorageSync('token')

    var token = wx.getStorageSync('token')
    const userInfo = {}
    userInfo.provinceId = this.data.province.id
    userInfo.cityId = this.data.city.id
    userInfo.districtId = this.data.county.id
    userInfo.linkMan = this.data.userName
    userInfo.mobile = this.data.phone
    userInfo.address = this.data.address
    userInfo.token = token
    console.log(userInfo)
    addAddress(userInfo).then(res => {
      console.log(res)
    })
    wx.navigateBack({
      delta: 1
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this
    wx.request({
      url: 'https://api.it120.cc/common/region/v2/province',
      success(res) {
        // console.log(res.data)
        that.setData({
          provinceList: res.data.data
        })
        console.log(that.data.provinceList)
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})